package com.nexus.nsh.service.cliente.action;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nexus.nsh.exception.NSHModelException;
import com.nexus.nsh.model.entity.Cliente;
import com.nexus.nsh.service.cliente.ClienteBuilder;
import com.nexus.nsh.service.cliente.ClienteService;

@SuppressWarnings("serial")
public class CadastrarClienteAction extends HttpServlet {

	String result = "";
	ClienteService service = new ClienteService();
	Logger log = Logger.getLogger(this.getClass().getSimpleName());

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		cadastrarCliente(req, resp);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		cadastrarCliente(req, resp);
	}

	private void cadastrarCliente(HttpServletRequest req,
			HttpServletResponse resp) throws IOException {

		result = "fail";

		log.info("Executando o Servlet");

		Cliente c = new Cliente();

		try {
			c = ClienteBuilder.buildCliente(req);
			service.cadastrarCliente(c);
			result = "success";
			log.info("Cadastro efetuado com sucesso");

		} catch (NSHModelException e) {
			log.severe(e.getMessage());
		}

		resp.getWriter().write(result);

	}

}
